<?php
namespace backend\controllers;

use Yii;
use yii\filters\AccessControl;
use yii\web\Controller;
use common\models\LoginForm;
use yii\filters\VerbFilter;

/**
 * Site controller
 */
class SiteController extends AController
{
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'rules' => [
                    [
                        'actions' => ['login', 'error','captcha'],
                        'allow' => true,
                    ],
                    [
                        'actions' => ['logout', 'index'],
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                ],
            ],
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    //'logout' => ['post'],
                ],
            ],
        ];
    }

    /**
     * @inheritdoc
     */
    public function actions()
    {
        return [
            'error' => [
                'class' => 'yii\web\ErrorAction',
            ],
            // 'captcha' => [
            //     'class' => 'yii\captcha\CaptchaAction',
            //     'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
            // ],
            'captcha' => [
                'class' => 'yii\captcha\CaptchaAction',
                'maxLength' => 4,
                'minLength' => 4,
            ],            

        ];
    }

    //首页

    public function actionIndex()
    {
        //1.订单量
        $order_sql = "SELECT COUNT(`id`) FROM `order` WHERE `order_status` IN (2,3,4)";
        $count_order = Yii::$app->db->createCommand($order_sql)->queryScalar();

        //2.用户量
        $user_sql = "SELECT COUNT(`id`) FROM `user`";
        $count_user = Yii::$app->db->createCommand($user_sql)->queryScalar();

        //3.商品总量
        $goods_sql = "SELECT COUNT(`id`) FROM `product` WHERE `is_show` = 1";
        $count_goods = Yii::$app->db->createCommand($goods_sql)->queryScalar();

        //4.充值金额
        $charge_sql = "SELECT SUM(`money`) FROM `recharge` WHERE `status` = 2";
        $count_charge = Yii::$app->db->createCommand($charge_sql)->queryScalar();

        return $this->render('index',[
            'count_order' => $count_order,
            'count_user' => $count_user,
            'count_goods' => $count_goods,
            'count_charge' => $count_charge,
        ]);
    }

    public function actionLogin()
    {
        if (!\Yii::$app->user->isGuest) {
            return $this->goHome();
        }

        $model = new LoginForm();
        if ($model->load(Yii::$app->request->post()) && $model->login()) {
            return $this->goBack();
        } else {
            return $this->render('login', [
                'model' => $model,
            ]);
        }
    }

    public function actionLogout()
    {
        Yii::$app->user->logout();

        return $this->goHome();
    }
}
